#encoding=utf8
'''
Created on 2012-10-15

@author: Administrator
'''
from dispatch.Processor import Processor
from pub import Func, RoomDict
from pub.Config import Config

class RUClass(Processor):
    '''
    指令：RU(room user)
    备注：房间用户列表
    格式：c-s:cmd#cmdID#roomID#[userOut]
     s-c:command#cmdID#roomID#userID#nickname#userPhoto#userIntegral#userOut
    '''


    def __init__(self,):
        Processor.__init__(self,);
        self.__roomId = 0;
        self.__userOut = None;
    
    def dataValidtor(self, cmdContent):
        if not cmdContent:
            return False;
        paraLen = len(cmdContent);
        if paraLen == 2:
            self.__roomId = Func.strToInt(cmdContent[0]);
            self.__userOut = cmdContent[1];
            return True;        
        return False;
    
    
    def outData(self,):
        
        if not Config.D_ROOM_DICT:
            Func.sendError(self._sockObj, self._cmdId,);
            return False;
        room = RoomDict.getRoomFromDictById(self.__roomId);
        if not room:
            Func.sendError(self._sockObj, self._cmdId, Config.ERROR_MESSAGE_ROOM_NULL + "");
            return False;
        users = room.get_user_list();
        if not users:
            Func.sendError(self._sockObj, self._cmdId, Config.ERROR_MESSAGE_ROOM_DETAIL_NULL + "");
            return False;
        sendStr = Func.toStr(self._cmd) + "#" + Func.toStr(self._cmdId) + "#" + Func.toStr(self.__roomId);
        gameOk = "";#游戏中
        gameOver = "";#淘汰
        gameSee = "";#旁观
        for user in users:
            tmp = "#%s#%s#%s#%s#%s" % (user.get_user_id(), user.get_user_nick_name(), user.get_user_photo(), user.get_user_cash(), user.get_user_game_state());
            state = Func.strToInt(user.get_user_game_state(), -1)
            if state == Config.I_USER_GAME_STATE_OK:
                gameOk += tmp
            elif state == Config.I_USER_GAME_STATE_OVER:
                gameOver += tmp;
            elif state == Config.I_USER_GAME_STATE_SEE:
                gameSee += tmp;
            else:
                pass;
        state = Func.strToInt(self.__userOut, -1)
        if state == Config.I_USER_GAME_STATE_OVER:
            sendStr += gameOver;           
        elif state == Config.I_USER_GAME_STATE_OK:
            sendStr += gameOk;
        elif state == Config.I_USER_GAME_STATE_SEE:
            sendStr += gameSee;
        else:
            sendStr += gameOk + gameSee + gameOver;
        
        self._sockObj.sendLine(str(sendStr)) 
        return Config.I_SYS_ONE;        
 
 
if __name__ == "__main__":
    pass;
    
  
